package com.dw.dwdzcommerce.view.mainContent;

import com.dw.dwdzcommerce.R;
import com.dw.dwdzcommerce.base.fragment.BaseFragment;
import com.dw.dwdzcommerce.base.persenter.BasePersenterInterface;
import com.dw.dwdzcommerce.entity.AdPage;
import com.dw.dwdzcommerce.entity.MainContentEntity;
import com.dw.dwdzcommerce.entity.Shop;
import com.dw.dwdzcommerce.entity.ShopImage;
import com.dw.dwdzcommerce.tools.commons.ArrayUtil;
import com.dw.dwdzcommerce.tools.uiView.scrollListView.ScrollListView;
import com.dw.dwdzcommerce.tools.uiView.xlistview.XListView.IXListViewListener;
import com.dw.dwdzcommerce.view.main.mainPersenter.mainPersenterInterface;
import com.dw.dwdzcommerce.view.mainContent.SelectLayout.OnItemClickListener;
import com.dw.dwdzcommerce.view.mainContent.mainContentPersenter.mainContentPersenterInterface;
import com.dw.dwdzcommerce.view.mainContent.ad.ADPagerLayout;
import com.dw.dwdzcommerce.view.mainContent.list.MainContentListAdapter;

import android.app.backup.RestoreObserver;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainContentFragment extends BaseFragment
		implements OnItemClickListener, IXListViewListener, mainContentPersenterInterface {
	private ScrollListView mListView;
	private ADPagerLayout mAdPagerLayout;
	private SelectLayout mSelectLayout;
	private mainContentPersenter mPersenter;

	@Override
	public int getViewLayout() {
		return R.layout.fragment_main_content;
	}

	@Override
	protected void initView(View view) {

		mListView = (ScrollListView) getActivity().findViewById(R.id.fragment_main_content_listv);
		// 置顶的广告栏
		mAdPagerLayout = new ADPagerLayout();
		mSelectLayout = new SelectLayout();
		mAdPagerLayout.initModule(this);
		mSelectLayout.initModule(this);
		mListView.setmListViewListener(this);
		MainContentEntity mData = generateTestData();
		MainContentListAdapter adapter = new MainContentListAdapter(mData, mAdPagerLayout, mSelectLayout,
				getActivity());

		mSelectLayout.setOnItemClickListener(this);
		mAdPagerLayout.initData(ArrayUtil.array2Lis(mData.getAdPages()));
		mListView.setAdapter(adapter);
		// mListView.addHeaderView(mAdPagerLayout.getADView());
		mListView.setOnFooterClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPersenter.onFootClick();
			}
		});

	}

	public static MainContentEntity generateTestData() {
		MainContentEntity e = new MainContentEntity();
		AdPage[] adPages = new AdPage[5];
		for (int i = 0; i < adPages.length; i++) {
			AdPage page = new AdPage();
			if (i == 0) {
				page.setAdPicUrl("http://img2.imgtn.bdimg.com/it/u=1075567296,1180227194&fm=21&gp=0.jpg");
			} else if (i == 1) {
				page.setAdPicUrl("http://img.taopic.com/uploads/allimg/140326/235113-1403260Q55222.jpg");
			} else if (i == 2) {
				page.setAdPicUrl("http://pic3.nipic.com/20090619/1242397_175120081_2.jpg");
			} else {
				page.setAdPicUrl("http://pic2.nipic.com/20090422/1242397_163111048_2.jpg");
			}

			adPages[i] = page;
		}

		e.setAdPages(adPages);
		Shop[] shops = new Shop[5];

		for (int i = 0; i < shops.length; i++) {
			Shop shop = new Shop();
			shop.setShopId(i);
			shop.setShopName("Nike");
			shop.setActRules("全场1折,清仓出售");
			shop.setAddress("远在天边近在眼前");
			shop.setPhone("1258082510");
			shop.setShopAvator("http://pic2.nipic.com/20090422/1242397_163111048_2.jpg");
			shop.setActTime("持续128天连续不中断!");
			if (i == 0) {
				ShopImage[] images = new ShopImage[2];
				shop.setActRules("全场1折,清仓出售,全场1折,清仓出售,全场1折,,全场1折,,全场1折");
				images[0] = new ShopImage("hello 这个性价比超高",
						"http://img.taopic.com/uploads/allimg/140326/235113-1403260Q55222.jpg");
				images[1] = new ShopImage("还行还行还行",
						"http://img.taopic.com/uploads/allimg/140326/235113-1403260Q55222.jpg");
				shop.setImages(images);
				shops[i] = shop;
			}
			if (i == 1) {
				ShopImage[] images = new ShopImage[2];

				images[0] = new ShopImage("hello 这个性价比超高", "http://pic2.nipic.com/20090422/1242397_163111048_2.jpg");
				images[1] = new ShopImage("还行还行还行", "http://pic2.nipic.com/20090422/1242397_163111048_2.jpg");
				shop.setImages(images);
				shops[i] = shop;
			} else {
				ShopImage[] images = new ShopImage[2];

				images[0] = new ShopImage("hello 这个性价比超高", "http://pic2.nipic.com/20090422/1242397_163111048_2.jpg");
				images[1] = new ShopImage("还行还行还行", "http://pic2.nipic.com/20090422/1242397_163111048_2.jpg");
				shop.setImages(images);
				shops[i] = shop;
			}
		}

		e.setShops(shops);
		return e;
	}

	@Override
	protected void bindView(View view) {
		mPersenter = new mainContentPersenter();
		mPersenter.bind(this);
	}

	@Override
	public void onViewGreate(View view, Bundle savedInstanceState) {

	}

	@Override
	public void onItemClcik(String name) {
		Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onRefresh() {

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				Handler mHandler = new Handler(Looper.getMainLooper()) {
					@Override
					public void handleMessage(Message msg) {
						mListView.stopRefresh();
					}
				};
				mHandler.sendEmptyMessage(1);
			}
		}, 1000);
	}

	@Override
	public void onLoadMore() {

	}

	@Override
	public void onPause() {
		super.onPause();
		mAdPagerLayout.stop();
	}

	static int totleNum;
	static int currIndex;

	@Override
	public void onResume() {
		super.onResume();
		restore();
	}

	private void restore() {
		mAdPagerLayout.restart();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mAdPagerLayout.stop();
	}

	@Override
	public void netWorkException(String msg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void remoteFailMsg(String msg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onComplete() {
		// TODO Auto-generated method stub

	}
	
	
	
	
}
